//websocket 
export function openSocket() {
    let url = 'ws://192.168.1.98:8181';
    let socket;
    // const WSMsg;
    if(typeof WebSocket === "undefined") {
        console.log("您的浏览器不支持socket");
        alert("您的浏览器不支持socket")
    } else {
        socket = new WebSocket(url);
        // let data;
        //打开事件
        socket.onopen = () => {
            console.log("websocket已打开");
        };
        //获得消息事件
        // socket.onmessage = (msg) => {
        //     console.log(msg.data, "后台返回的数据")
        //     // console.log(msg)
        //     getmsg(msg.data);
        //     return msg.data;
        // };
        socket.onmessage =getmsg;
        //关闭事件  (websocket可在服务端配置连接超时时间)
        socket.onclose = function (e) {
            console.log("websocket已关闭");
            //打印错误信息
            console.log(
                "websocket 断开: " + e.code + " " + e.reason + " " + e.wasClean
            );
            console.log(e);
        };
        //发生了错误事件
        socket.onerror = function () {
            console.log("websocket发生了错误");
            //连接建立失败重连
            openSocket();
        };
    }
    return socket;
    
}

export function getmsg(data){
    console.log(data,"getmsg方法输出")
    return data;
}